#include "t6Printer.h"
#include <windows.h>
#include <winspool.h>

t6Printer::t6Printer()
{
    Handle = INVALID_HANDLE_VALUE;
}

t6Printer::~t6Printer()
{
	close();
}

bool t6Printer::defaultPrinter(QString &name)
{
    DWORD len = 0;
    wchar_t *tmp;

	GetDefaultPrinter(NULL, &len);
	if (len == 0)
		return false;

	tmp = new wchar_t[len + 1];
	if (!GetDefaultPrinter(tmp, &len))
	{
		delete tmp;
        return false;
	}
    name = name.fromWCharArray(tmp, len);
	delete tmp;
    dprinter = name;

    return true;
}

bool t6Printer::open(QString &name)
{
    if (OpenPrinter((LPWSTR)name.toStdWString().data(), &Handle, NULL))
        return true;
    else
        return false;
}

void t6Printer::close()
{
	ClosePrinter(Handle);
    Handle = INVALID_HANDLE_VALUE;
}

int t6Printer::status()
{
	DWORD dwNeeded = 0;
	PRINTER_INFO_6 * pPrinterInfo = NULL;
	int ret = -1;

    if (Handle == INVALID_HANDLE_VALUE)
    {
        QString name;

        defaultPrinter(name);
        open(name);
    }
    if (Handle == INVALID_HANDLE_VALUE)
        return ret;

	GetPrinter(Handle, 6, NULL, 0, &dwNeeded);
	if (dwNeeded == 0)
		return ret;

	pPrinterInfo = (PRINTER_INFO_6 *)malloc(dwNeeded);
	if (GetPrinter(Handle, 6, (LPBYTE)pPrinterInfo, dwNeeded, &dwNeeded))
	{
		ret = pPrinterInfo->dwStatus;
	}

	free(pPrinterInfo);

	return ret;
}

bool t6Printer::printRaw(QByteArray &data)
{
    DWORD len;
	DOC_INFO_1  DocInfo;
	DWORD      dwJob = 0L;

    if (Handle == INVALID_HANDLE_VALUE)
    {
        QString name;

        defaultPrinter(name);
        open(name);
    }
    if (Handle == INVALID_HANDLE_VALUE)
        return false;

    DocInfo.pDocName = (LPTSTR)L"hndoc";
	DocInfo.pOutputFile = NULL;
	DocInfo.pDatatype = (LPTSTR)L"RAW";

	dwJob = StartDocPrinter(Handle, 1, (LPBYTE)&DocInfo);
	if (dwJob > 0)
	{
        StartPagePrinter(Handle);
        WritePrinter(Handle, data.data(), data.size(), &len);
	    EndPagePrinter(Handle);
	}
	EndDocPrinter(Handle);

	return true;
}

QByteArray t6Printer::makeHead()
{
    QByteArray buf = "~CREATE;FORM-0;226\r\n" \
    "SCALE;DOT;300;300\r\n"\
    "ISET;'UTF8'\r\n"\
    "FONT;NAME RSCUHEIR.TTF\r\n";

    return buf;
}

QByteArray t6Printer::makeEnd()
{
    QByteArray buf = "END\r\n" \
            "~EXECUTE;FORM-0;1\r\n" \
            "~NORMAL\r\n"\
		    "~DELETE FORM;FORM-0\r\n";

    return buf;
}

QByteArray t6Printer::makeText(int x, int y, int fs, QString t)
{
    QByteArray buf;
    QString str;
	QString cmd;

	cmd = "ALPHA\r\n";
    str = str.sprintf("POINT;%d;%d;%d;%d;*%s*\r\n", y, x, fs, fs,
                      t.toStdString().data());
	cmd += str;
	cmd += "STOP\r\n";
    buf = cmd.toStdString().data();

    return buf;
}

QByteArray t6Printer::makeQrCode(int x, int y, int fs, QString t)
{
    QByteArray buf;
    QString str;
	QString cmd;

	cmd = "BARCODE\r\n";
    str = str.sprintf("QRCODE;INV;XD4;T2;E0;M0;I0;%d;%d\r\n", y, x);
	cmd += str;
	str = str.sprintf("\"%s\"\r\n", t.toStdString().data());
	cmd += str;
	cmd += "STOP\r\n";

    buf = cmd.toStdString().data();

    return buf;
}

